import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/components/MyIconButton.dart';
import 'package:toys_flutter/pages/ChatRoom.dart';

class MessagesPage extends StatefulWidget {
  const MessagesPage({super.key});

  @override
  _State createState() => _State();
}

class _State extends State<MessagesPage> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    _tabController.addListener(() {
      setState(() {}); // 监听 Tab 的变化，更新状态
    });
  }

  @override
  void dispose() {
    _tabController.dispose(); // 释放控制器
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const Color colorMain = Color.fromRGBO(183, 235, 143, 1);

    return Scaffold(
      appBar: AppBar(
        title: SizedBox(
          width: 120,
          child: TDTabBar(
            indicatorPadding: EdgeInsets.zero,
            labelPadding: EdgeInsets.zero,
            tabs: const [
              TDTab(text: '信息'),
              TDTab(text: '聊天室'),
            ],
            controller: _tabController,
            showIndicator: true,
            dividerHeight: 0,
          ),
        ),
        backgroundColor: colorMain,
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 15),
            child: MyIconButton(
                icon: Icons.settings,
                onPressed: () {
                  print('设置');
                }),
          ),
        ],
      ),
      body: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              colorMain,
              Colors.white,
              Colors.white,
              Colors.white,
            ],
          ),
        ),
        child: TabBarView(
          controller: _tabController, // 添加控制器
          physics: const NeverScrollableScrollPhysics(),
          children: const [
            Center(
              child: Text('信息'),
            ),
            ChatRoom()
          ],
        ),
      ),
    );
  }
}
